package com.sinosoft.cnmuseum.servlets;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.dao.MbGyTcDao;
import com.sinosoft.cnmuseum.dao.MbXtglJsglDao;
import com.sinosoft.cnmuseum.dao.MbXtglRyjsfpDao;
import com.sinosoft.cnmuseum.dao.MbXtglYhxxDao;
import com.sinosoft.cnmuseum.entity.MbGyTc;
import com.sinosoft.cnmuseum.entity.MbXtglJsgl;
import com.sinosoft.cnmuseum.entity.MbXtglRyjsfp;
import com.sinosoft.cnmuseum.entity.MbXtglYhxx;
import com.sinosoft.cnmuseum.servlets.i.IServlet;
import com.sinosoft.cnmuseum.utils.LogUtils;
import com.sinosoft.cnmuseum.utils.PageUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.TransAnnotation;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

/**
 * Title: MbXtglZjServlet
 * Description: 专家管理
 * @author WEWE
 * @date 2019年1月10日 下午2:37:45
 * @version 1.0
 */
public class MbXtglZjServlet implements IServlet {
  private static Logger logger = Logger.getLogger(MbXtglZjServlet.class);

  /**
   * Description: 专家列表
   * @param request
   * @param response
   * @param data
   * @return
   */
  @Override
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getData(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
      int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
      String countsql = "select count(zdbh) as count from mb_xtgl_yhxxzj_view where " + MbXtglYhxx.SFWZJ + "=1 ";
      String sql = "select * from mb_xtgl_yhxxzj_view where " + MbXtglYhxx.SFWZJ + "=1 ";
      String ordersql = " order by zdbh desc ";
      String limitsql = " limit ";

		String szjg = data.getStrValue(MbXtglYhxx.SZJG);
		String xm = data.getStrValue(MbXtglYhxx.XM);
		String zjlx = data.getStrValue(MbXtglYhxx.ZJLX);
		String wybm = data.getStrValue(MbXtglYhxx.WYBM);
		String xb = data.getStrValue(MbXtglYhxx.XB);
		String mz = data.getStrValue(MbXtglYhxx.MZ);
		String csrq = data.getStrValue(MbXtglYhxx.CSRQ);
      if (StringUtils.isNoEmpty(xm)) {
        countsql += " and "+ MbXtglYhxx.XM + " = '" + xm + "'";
        sql += " and "+ MbXtglYhxx.XM + " = '" + xm + "'";
      }
      if(StringUtils.isNoEmpty(szjg)){
			countsql += " and "+MbXtglYhxx.SZJG+" = "+szjg+" ";
			sql += " and "+MbXtglYhxx.SZJG+" = "+szjg+" ";
		}
		if(StringUtils.isNoEmpty(xm)){
			countsql += " and "+MbXtglYhxx.XM+" like '%"+xm+"%' ";
			sql += " and "+MbXtglYhxx.XM+" like '%"+xm+"%' ";
		}
		if(StringUtils.isNoEmpty(zjlx)){
			countsql += " and "+MbXtglYhxx.ZJLX+" = "+zjlx+" ";
			sql += " and "+MbXtglYhxx.ZJLX+" = "+zjlx+" ";
		}
		if(StringUtils.isNoEmpty(wybm)){
			countsql += " and "+MbXtglYhxx.WYBM+" like '%"+wybm+"%'";
			sql += " and "+MbXtglYhxx.WYBM+" like '%"+wybm+"%'";
		}
		if(StringUtils.isNoEmpty(xb)){
			countsql += " and "+MbXtglYhxx.XB+" = "+xb+" ";
			sql += " and "+MbXtglYhxx.XB+" = "+xb+" ";
		}
		if(StringUtils.isNoEmpty(mz)){
			countsql += " and "+MbXtglYhxx.MZ+" = "+mz+" ";
			sql += " and "+MbXtglYhxx.MZ+" = "+mz+" ";
		}
		if(StringUtils.isNoEmpty(csrq)){
			countsql += " and "+MbXtglYhxx.CSRQ+" = "+csrq+"";
			sql += " and "+MbXtglYhxx.CSRQ+" = "+csrq+"";
		}
      
      logger.info("CSQL:" + countsql);
      List<Record> countlist = dao.getBySql(countsql);
      int total = countlist.get(0).getIntValue("count");
      PageUtil page = new PageUtil(pagenumber, pagesize, total);
      limitsql += page.getStart()+","+pagesize;
      logger.info("SQL:" + sql+ordersql+limitsql);
      List<Record> list = dao.getBySql(sql+ordersql+limitsql);
      // 组装数据
      result.add(Status.ROWS, list);
      result.add(Status.TOTAL, total);
      result.add(Status.PAGENUMBER, pagenumber);
      result.add(Status.PAGESIZE, pagesize);
      result.add(Status.PAGETOTAL, page.getPagetotal());
      // 返回接口调用成功标识
      result.add(Status.SATUS,Status.OK);
    } catch (Exception e) {
      // 返回接口调用失败标识
      result.add(Status.SATUS,Status.FAILED);
      // 返回接口调用失败信息，需要自己添加
      result.add(Status.MSG,"查询失败！");
    }
    // 接口返回Record对象，框架会自动封装返回给请求result对象中的数据
    return result;
  }
  
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getAllZjList(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      String countsql = "select count(zdbh) as count from " + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " + MbXtglYhxx.SFWZJ + "=1 ";
      String sql = "select * from " + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " + MbXtglYhxx.SFWZJ + "=1 ";
      String ordersql = " order by zdbh desc ";

      String xm = data.getStrValue(MbXtglYhxx.XM);
      if (StringUtils.isNoEmpty(xm)) {
        countsql += " and "+ MbXtglYhxx.XM + " = '" + xm + "'";
        sql += " and "+ MbXtglYhxx.XM + " = '" + xm + "'";
      }
      logger.info("CSQL:" + countsql);
      List<Record> countlist = dao.getBySql(countsql);
      int total = countlist.get(0).getIntValue("count");
      logger.info("SQL:" + sql+ordersql);
      List<Record> list = dao.getBySql(sql+ordersql);
      // 组装数据
      result.add(Status.ROWS, list);
      result.add(Status.TOTAL, total);
      // 返回接口调用成功标识
      result.add(Status.SATUS,Status.OK);
    } catch (Exception e) {
      // 返回接口调用失败标识
      result.add(Status.SATUS,Status.FAILED);
      // 返回接口调用失败信息，需要自己添加
      result.add(Status.MSG,"查询失败！");
    }
    // 接口返回Record对象，框架会自动封装返回给请求result对象中的数据
    return result;
  }
  /**
   * Description: 添加专家
   * @param request
   * @param response
   * @param data
   * @return
   */
  @Override
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record insert(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try{
        HttpSession session = request.getSession(false);
        String contributor = (String) session.getAttribute(Status.SESSION_USERID);//责任者
        
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      Record yhxx = new Record();
      String xm = data.getStrValue(MbXtglYhxx.XM);
      String yhmm = data.getStrValue(MbXtglYhxx.YHMM);
      String szjg = data.getStrValue(MbXtglYhxx.SZJG);
      String zjlx = data.getStrValue(MbXtglYhxx.ZJLX);
      String zjhm = data.getStrValue(MbXtglYhxx.ZJHM);
      String gj = data.getStrValue(MbXtglYhxx.GJ);
      String xb = data.getStrValue(MbXtglYhxx.XB);
      String mz = data.getStrValue(MbXtglYhxx.MZ);
      String csrq = data.getStrValue(MbXtglYhxx.CSRQ);
      String dzyj = data.getStrValue(MbXtglYhxx.DZYJ);
      String dh = data.getStrValue(MbXtglYhxx.DH);
      String zjms = data.getStrValue(MbXtglYhxx.ZJMS);
      String yjtc = data.getStrValue(MbXtglYhxx.YJTC);
      String wybm = data.getStrValue(MbXtglYhxx.WYBM);
      yhxx.add(MbXtglYhxx.SFWZJ, 1);
      if(StringUtils.isNoEmpty(xm)){
        yhxx.add(MbXtglYhxx.XM, xm);
      }
      if(StringUtils.isNoEmpty(yhmm)){
        yhxx.add(MbXtglYhxx.YHMM, yhmm);
      }
      if(StringUtils.isNoEmpty(szjg)){
        yhxx.add(MbXtglYhxx.SZJG, szjg);
      }
      if(StringUtils.isNoEmpty(zjlx)){
        yhxx.add(MbXtglYhxx.ZJLX, zjlx);
      }
      if(StringUtils.isNoEmpty(zjhm)){
        yhxx.add(MbXtglYhxx.ZJHM, zjhm);
      }
      if(StringUtils.isNoEmpty(gj)){
        yhxx.add(MbXtglYhxx.GJ, gj);
      }
      if(StringUtils.isNoEmpty(xb)){
        yhxx.add(MbXtglYhxx.XB, xb);
      }
      if(StringUtils.isNoEmpty(mz)){
        yhxx.add(MbXtglYhxx.MZ, mz);
      }
      if(StringUtils.isNoEmpty(csrq)){
        yhxx.add(MbXtglYhxx.CSRQ, csrq);
      }
      if(StringUtils.isNoEmpty(dzyj)){
        yhxx.add(MbXtglYhxx.DZYJ, dzyj);
      }
      if(StringUtils.isNoEmpty(dh)){
        yhxx.add(MbXtglYhxx.DH, dh);
      }
      if(StringUtils.isNoEmpty(zjms)){
        yhxx.add(MbXtglYhxx.ZJMS, zjms);
      }
      if(StringUtils.isNoEmpty(yjtc)){
        yhxx.add(MbXtglYhxx.YJTC, yjtc);
      }
      if(StringUtils.isNoEmpty(wybm)){
        yhxx.add(MbXtglYhxx.WYBM, wybm);
      }
//      if(StringUtils.isNoEmpty(zjtcs)){//专家特长
//        StringBuffer zjtcIds = new StringBuffer();;
//        String[] zjtcSpl = zjtcs.split(",");
//        for (String zjtc : zjtcSpl) {
//          int zjtcId = 0;
//          MbGyTcDao tcDao = new MbGyTcDao();
//          List<Record> tcs = tcDao.getBySql("select * from " + MbGyTc.MB_GY_TC_TABLE_NAME + " where "+ MbGyTc.NAME + "='" + zjtc + "'");
//          if (tcs != null && tcs.size() >0) {
//            Record tc = tcs.get(0);
//            zjtcId = tc.getIntValue(MbGyTc.ZDBH);
//          } else {
//            Record tc = new Record();
//            tc.add(MbGyTc.NAME, zjtc);
//            tcDao.insert(tc);
//            zjtcId = tc.getIntValue(MbGyTc.ZDBH);
//          }
//          zjtcIds.append(zjtcId).append(",");
//        }
//        if (zjtcIds.toString().endsWith(","))
//          zjtcIds.deleteCharAt(zjtcIds.length()-1);
//        yhxx.add(MbXtglYhxx.YJTC, zjtcIds.toString());
//      }
      if(dao.insert(yhxx)){
          LogUtils logUtils = new LogUtils();
          String desc = "专家"+LogUtils.MbXtglYhxx_INSERT_DESC;
          logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_yhgl, contributor, desc);
        result.add(Status.SATUS,Status.OK);
      }else{
        throw new Exception("插入信息失败！");
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
      // 事务回滚，事务模式下，此标识将使此次事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }

  /**
   * Description: 专家详细
   * @param request
   * @param response
   * @param data
   * @return
   */
  @rquestMethod(MethodNote = ReqMode.All)
  public Record getDataById(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      Record yhxx = dao.getById(data.getIntValue("zdbh"));
      if(yhxx!=null){
        String yjtc = yhxx.getStrValue(MbXtglYhxx.YJTC);
        String sql = "SELECT GROUP_CONCAT(" + MbGyTc.NAME + ") as yjtcname FROM " + MbGyTc.MB_GY_TC_TABLE_NAME + " WHERE " + MbGyTc.ZDBH + " in (" + yjtc + ")";
        MbGyTcDao yjtcDao = new MbGyTcDao();
        List<Record> rs = yjtcDao.getBySql(sql);
        if (rs != null && rs.size() > 0) {
          Record r = rs.get(0);
          yhxx.add("yjtcname", r.getStrValue("yjtcname"));
        }
        result.add(Status.ROWS, yhxx);
        result.add(Status.SATUS,Status.OK);
      }else{
        throw new Exception("查询信息失败！");
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }

  /**
   * Description: 更新专家
   * @param request
   * @param response
   * @param data
   * @return
   */
  @Override
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record update(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      Record yhxx = dao.getById(data.getIntValue("zdbh"));
      if(yhxx!=null){
          HttpSession session = request.getSession(false);
          String contributor = (String) session.getAttribute(Status.SESSION_USERID);//责任者
          
        String xm = data.getStrValue(MbXtglYhxx.XM);
        String yhmm = data.getStrValue(MbXtglYhxx.YHMM);
        String szjg = data.getStrValue(MbXtglYhxx.SZJG);
        String zjlx = data.getStrValue(MbXtglYhxx.ZJLX);
        String zjhm = data.getStrValue(MbXtglYhxx.ZJHM);
        String gj = data.getStrValue(MbXtglYhxx.GJ);
        String xb = data.getStrValue(MbXtglYhxx.XB);
        String mz = data.getStrValue(MbXtglYhxx.MZ);
        String csrq = data.getStrValue(MbXtglYhxx.CSRQ);
        String dzyj = data.getStrValue(MbXtglYhxx.DZYJ);
        String dh = data.getStrValue(MbXtglYhxx.DH);
        String zjms = data.getStrValue(MbXtglYhxx.ZJMS);
        String yjtc = data.getStrValue(MbXtglYhxx.YJTC);
        String wybm = data.getStrValue(MbXtglYhxx.WYBM);
        yhxx.add(MbXtglYhxx.SFWZJ, 1);
        if(StringUtils.isNoEmpty(xm)){
          yhxx.add(MbXtglYhxx.XM, xm);
        }
        if(StringUtils.isNoEmpty(yhmm)){
          yhxx.add(MbXtglYhxx.YHMM, yhmm);
        }
        if(StringUtils.isNoEmpty(szjg)){
          yhxx.add(MbXtglYhxx.SZJG, szjg);
        }
        if(StringUtils.isNoEmpty(zjlx)){
          yhxx.add(MbXtglYhxx.ZJLX, zjlx);
        }
        if(StringUtils.isNoEmpty(zjhm)){
          yhxx.add(MbXtglYhxx.ZJHM, zjhm);
        }
        if(StringUtils.isNoEmpty(gj)){
          yhxx.add(MbXtglYhxx.GJ, gj);
        }
        if(StringUtils.isNoEmpty(xb)){
          yhxx.add(MbXtglYhxx.XB, xb);
        }
        if(StringUtils.isNoEmpty(mz)){
          yhxx.add(MbXtglYhxx.MZ, mz);
        }
        if(StringUtils.isNoEmpty(csrq)){
          yhxx.add(MbXtglYhxx.CSRQ, csrq);
        }
        if(StringUtils.isNoEmpty(dh)){
          yhxx.add(MbXtglYhxx.DH, dh);
        }
        if(StringUtils.isNoEmpty(dzyj)){
          yhxx.add(MbXtglYhxx.DZYJ, dzyj);
        }
        if(StringUtils.isNoEmpty(zjms)){
          yhxx.add(MbXtglYhxx.ZJMS, zjms);
        }
        if(StringUtils.isNoEmpty(yjtc)){
          yhxx.add(MbXtglYhxx.YJTC, yjtc);
        }
        if(StringUtils.isNoEmpty(wybm)){
          yhxx.add(MbXtglYhxx.WYBM, wybm);
        }
        if(dao.update(yhxx)){
            LogUtils logUtils = new LogUtils();
            String desc = "专家"+LogUtils.MbXtglYhxx_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_yhgl, contributor, desc);
          result.add(Status.SATUS,Status.OK);
        }else{
          throw new Exception("更新信息失败！");
        }
      }else{
        throw new Exception("更新信息失败！");
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }
  /**
   * Description: 删除专家
   * @param request
   * @param response
   * @param data
   * @return
   */
  @Override
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record delete(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    result.add(Status.SATUS,Status.OK);
    try {
        HttpSession session = request.getSession(false);
        String contributor = (String) session.getAttribute(Status.SESSION_USERID);//责任者
        
      MbXtglYhxxDao dao = new MbXtglYhxxDao();
      Record yhxx = dao.getById(data.getIntValue("zdbh"));
      if(yhxx!=null){
    	  yhxx.add(MbXtglYhxx.ZHUXIAO, 1);
		  dao.update(yhxx);
		  
          LogUtils logUtils = new LogUtils();
          String desc = LogUtils.MbXtglYhxx_DELETE_DESC;
          logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_yhgl, contributor, desc);
		  
      }
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      // 事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }

  /**
   * Description: 分配角色，多个专家，一个角色
   * @param request
   * @param response
   * @param data
   * @return
   */
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record assignJs(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      String userid = data.getStrValue("userid");
      String[] rybmList = userid.split(",");
      String roleid = data.getStrValue("roleid");
      if(StringUtils.isEmpty(roleid)||StringUtils.isEmpty(userid)){
    	  throw new Exception("传递参数为空");
      }
      MbXtglRyjsfpDao dao = new MbXtglRyjsfpDao();
      for (String rybm : rybmList) {
        HashMap<String, Object> map = new HashMap<String, Object>(){{
          put(MbXtglRyjsfp.RYBM, rybm);
          put(MbXtglRyjsfp.JSBM, roleid);
        }};
        List<Record> old = dao.getListByProperty(map);
        if(old!=null&&old.size()>0){
            dao.delete(old);
        }else{
            if(StringUtils.isNoEmpty(roleid)){
                Record ryjsfp = new Record();
                ryjsfp.add(MbXtglRyjsfp.JSBM, roleid);
                ryjsfp.add(MbXtglRyjsfp.RYBM, rybm);
                dao.insert(ryjsfp);
              } 
        }       
      }
      result.add(Status.SATUS,Status.OK);
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      // 事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }

  /**
   * Description: 人员的角色列表
   * @param request
   * @param response
   * @param data
   * @return
   */
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getJs(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      String rybm = data.getStrValue("userid");
      MbXtglJsglDao dao = new MbXtglJsglDao();
      StringBuffer sql = new StringBuffer("select * from " + MbXtglJsgl.MB_XTGL_JSGL_TABLE_NAME);
      sql.append(" where " + MbXtglJsgl.ZDBH + " in (");
      sql.append(" select " + MbXtglRyjsfp.JSBM + " from " + MbXtglRyjsfp.MB_XTGL_RYJSFP_TABLE_NAME);
      sql.append(" where " + MbXtglRyjsfp.RYBM + " = '" + rybm + "')");
      logger.info("SQL:" + sql.toString());
      List<Record> list = dao.getBySql(sql.toString());
      result.add(Status.ROWS, list);
      result.add(Status.TOTAL, list.size());
      result.add(Status.SATUS,Status.OK);
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      // 事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }
  
  /**
   * Description: 人员的角色列表
   * @param request
   * @param response
   * @param data
   * @return
   */
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getJsByUserOnline(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      if(request.getSession().getAttribute(Status.USERID)!=null){
        String rybm = request.getSession().getAttribute(Status.USERID).toString();
        
        MbXtglJsglDao dao = new MbXtglJsglDao();
        StringBuffer sql = new StringBuffer("select * from " + MbXtglJsgl.MB_XTGL_JSGL_TABLE_NAME);
        sql.append(" where " + MbXtglJsgl.JSBM + " in (");
        sql.append(" select " + MbXtglRyjsfp.JSBM + " from " + MbXtglRyjsfp.MB_XTGL_RYJSFP_TABLE_NAME);
        sql.append(" where " + MbXtglRyjsfp.RYBM + " = '" + rybm + "')");
        logger.info("SQL:" + sql.toString());
        List<Record> list = dao.getBySql(sql.toString());
        result.add(Status.ROWS, list);
        result.add(Status.TOTAL, list.size());
        result.add(Status.SATUS,Status.OK);
      }else{

        result.add(Status.SATUS, Status.FAILED);
      }
      
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      // 事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }
  /**
   * Description: 添加特长
   * @param request
   * @param response
   * @param data
   * @return
   */
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record addYjtc(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      String name = data.getStrValue(MbGyTc.NAME);
      MbGyTcDao dao = new MbGyTcDao();
      Record yjtc = new Record();
      yjtc.add(MbGyTc.NAME, name);
      yjtc.add(MbGyTc.SCBZ, 0);
      if(dao.insert(yjtc)){
        result.add(Status.SATUS,Status.OK);
      }else{
        throw new Exception("插入信息失败！");
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
      // 事务回滚，事务模式下，此标识将使此次事务回滚
      result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }

  public static void main(String[] args) {
    // 每月税额
    double[] tax = new double[12];
    // 税额总和
    double sum = 0;
    // 预扣率档位
    int[] ykl_y = {36000, 144000, 300000, 420000, 660000, 960000, Integer.MAX_VALUE};
    // 预扣率
    int[] ykl = {3, 10, 20, 25, 30, 35, 45};
    // 速算扣除数
    int[] sskcs = {0, 2520, 16920, 31920, 52920, 85920, 181920};
    // 公积金上限
    int gjj_max = 3048;

    int month  = 12;
    // 月收入税前
    int income = 6000;
    // 专项扣除
    int zxkc = 0;

    for (int i=1; i<=month; i++) {
      // 养老
      double yal = income*0.08;
      // 医疗
      double yil = income*0.02;
      // 失业
      double sy = income*0.002;
      // 公积金
      double gjj = income*0.12 < gjj_max ? income*0.12 : gjj_max;
      double t = (income-5000-yal-yil-sy-gjj-zxkc)*i;
      int index = 0;
      for (int j=0; j<12; j++) {
        if (t<ykl_y[j]) {
          index = j;
          break;
        }
      }
      double result =  t*ykl[index]/100-sskcs[index];
      result = result - sum;
      sum = sum + result;
      tax[i-1] = result;
      System.out.println(i + "月：" + result);
    }
    System.out.println("总计：" + sum);
  }

}
